<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="缴费人" prop="payer">
        <el-input
          v-model="queryParams.payer"
          placeholder="请输入缴费人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收费日期"  prop="payerDate">
        <el-date-picker
          v-model="queryParams.payerDate"
          style="width: 240px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择收费日期"
          @keyup.enter.native="handleQuery">

        </el-date-picker>
      </el-form-item>
      <el-form-item label="收费类型" label-width="auto">
        <el-select
          v-model="queryParams.chargeType"
          placeholder="请选择收费类型"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in dict.type.community_charge_type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>

        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:charge:add']">新增</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row>
        <el-form-item label="水费合计:">
          {{ waterPay }}元
        </el-form-item>
        <el-form-item label="电费合计:">
          {{ electricityPay }}元
        </el-form-item>
        <el-form-item label="物业费合计:">
          {{ propertyPay }}元
        </el-form-item>
        <el-form-item label="总缴费:">
          {{ totalPay }}元
        </el-form-item>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="收费编号" align="center" prop="chargeId"/>
      <el-table-column label="收费类型" align="center" prop="chargeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.community_charge_type" :value="scope.row.chargeType"/>
        </template>
      </el-table-column>
      <el-table-column label="缴费人" align="center" prop="payer"/>
      <el-table-column label="缴费人联系电话" align="center" prop="payerMobile"/>
      <el-table-column label="收费金额" align="center" prop="chargeAmount"/>
      <el-table-column label="缴费日期" align="center" prop="payerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:charge:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:charge:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  添加分页组件,传入分页变量  -->
    <!--
       1.首先总条数初始化时为0，当调用后台接口，返回的数据大于0时，才显示数据
       2.这个.sync主要做的事情就是实现父子组件之间的双向绑定，双向绑定的意思就是父亲变化，儿子也随之变化，
       在当前页面上举个例子，查询参数这个方法的数量变化，子组件显示的正确的页码记录数
       3.@pagination="getList",当页码或每页显示的数据发生变化时，再重新调用查询方法，重新获取数据列表。
       -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改charge对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收费类型" label-width="auto">
          <el-select
            v-model="form.chargeType"
            placeholder="请选择收费类型"
            clearable
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in dict.type.community_charge_type"
              :key="item.value"
              :label="item.label"
              :value="Number(item.value) ">

            </el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="缴费人" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入缴费人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="payerMobile">
          <el-input v-model="form.payerMobile" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="收费金额" prop="chargeAmount">
          <el-input v-model="form.chargeAmount" placeholder="请输入收费金额"/>
        </el-form-item>
        <el-form-item label="缴费日期" prop="payerDate">
          <el-date-picker clearable
                          v-model="form.payerDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择缴费日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCharge, getCharge, delCharge, addCharge, updateCharge} from "@/api/system/charge";

export default {
  name: "Charge",
  // 导入字典表中的数据
  dicts: [`community_charge_type`],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // charge表格数据
      chargeList: [],
      // 收费类型
      typeOption: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // 页码
        pageNum: 100,
        // 每页显示的数据数量
        pageSize: 5,
        chargeType: null,
        payer: null,
        payerMobile: null,
        chargeAmount: null,
        chargeDate: null,
        payerDate: null,
      },
      // 计算合计
      members: [],   // === res.memberInfo
      sum: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createBy: [
          {required: true, message: "收费人不能为空", trigger: "blur"}
        ],
        chargeType: [
          {required: true, message: "收费类型不能为空", trigger: "blur"}
        ],
        payer: [
          {required: true, message: "缴费人不能为空", trigger: "blur"}
        ],
        payerMobile: [
          { required: true,
            pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
            message: '联系电话不能为空，并且输入至少11位号码', trigger: 'change'},
        ],
        chargeAmount: [
          {  pattern: /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/,
            message: "请输入正确金额,必须是数字型,且小数位最多保留两位！",
            trigger: "blur"},
        ],
        payerDate: [
          {required: true, message: "缴费日期必须添加", trigger: "change"}
        ],
      }
    };
  },
  // 需要异步查询
  created() {
    this.getList();
  },
  // 计算属性
  computed: {
    // 总缴费
    totalPay() {
      return this.chargeList.reduce((sum, charge) => sum + charge.chargeAmount, 0).toFixed(2)
    },
    // 电费
    electricityPay() {
      // filter(item => item.chargeType === 0) => 过滤器
      // 1.先通过 过滤器将电费的类型
      return this.chargeList.filter(item => item.chargeType === 0).reduce((sum, charge) => sum + charge.chargeAmount, 0).toFixed(2)
    },
    // 水费
    waterPay() {
      return this.chargeList.filter(item => item.chargeType === 2).reduce((sum, charge) => sum + charge.chargeAmount, 0).toFixed(2)
    },
    // 物业费
    propertyPay() {
      return this.chargeList.filter(item => item.chargeType === 1).reduce((sum, charge) => sum + charge.chargeAmount, 0).toFixed(2)
    }
  },
  methods: {
    /** 查询charge列表 */
    getList() {
      this.loading = true;
      // 调用后台方法，传入查询参数这个对象，等数据全部查询成功后，再执行then里面的内容
      listCharge(this.queryParams).then(response => {
        this.chargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chargeId: null,
        createBy: null,
        chargeType: null,
        payer: null,
        payerMobile: null,
        chargeAmount: null,
        // chargeDate: null,
        payerDate: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      debugger;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chargeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chargeId = row.chargeId || this.ids
      getCharge(chargeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收费";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.chargeId != null) {
            updateCharge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCharge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chargeIds = row.chargeId || this.ids;
      this.$modal.confirm('是否确认删除charge编号为"' + chargeIds + '"的数据项？').then(function () {
        return delCharge(chargeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/charge/export', {
        ...this.queryParams
      }, `charge_${new Date().getTime()}.xlsx`)
    }
  },

};
</script>

<style scoped>

.borderNone /deep/ .el-input__inner {
  border: none;
}
</style>
